package me.youline.dataServer.utils;

public class IdPagination {

	public static enum Order {
		desc, asc
	}

	/**
	 * 起始ID
	 */
	private Long startId = NULL_ID_VALUE;

	/**
	 * 结束ID
	 */
	private Long endId = NULL_ID_VALUE;

	/**
	 * 页面大小
	 */
	private int pageSize = 20;

	private Order order = Order.desc;

	/**
	 * 空值标识
	 */
	public static final Long NULL_ID_VALUE = -1L;

	public static final int UNLIMIT_PAGE_SIZE = -1;

	private IdPagination() {

	}

	public static IdPagination getPageSizePagination(int pageSize) {
		IdPagination p = new IdPagination();
		p.startId = NULL_ID_VALUE;
		p.endId = NULL_ID_VALUE;
		p.pageSize = pageSize;
		return p;
	}

	public static IdPagination getCompleteIdPagination(Long startId,
			Long endId, int pageSize) {
		if (pageSize <= 0
				|| ((startId != null && endId != null) && startId.equals(endId))
				|| ((startId != null && startId < 0) && (endId != null && endId < 0))) {
			throw new RuntimeException("无法创建分页数据,其起始id必须有效,且分页数据大于0");
		}
		IdPagination p = new IdPagination();
		if (startId != null) {
			p.startId = startId;
		}
		if (endId != null) {
			p.endId = endId;
		}
		p.pageSize = pageSize;
		return p;
	}


	private IdPagination(Long startId, Long endId, int pageSize) {

	}

	public int getPageSize() {
		return this.pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public Long getStartId() {
		return startId;
	}

	public void setStartId(Long fromId) {
		this.startId = fromId;
	}

	public Long getEndId() {
		return endId;
	}

	public void setEndId(Long endId) {
		this.endId = endId;
	}

	/**
	 * 是否有起始ID
	 * ID值不为null 且不为空值（-1）且id值大于0
	 * @return
	 */
	public boolean hasStartId() {
		return this.startId != null && !NULL_ID_VALUE.equals(this.startId) && this.startId >= 0L;
	}

	/**
	 * 是否存在结束ID
	 * ID值不为null 且不为空值（-1）且id值大于0
	 * @return
	 */
	public boolean hasEndId() {
		return this.startId != null && !NULL_ID_VALUE.equals(this.endId) && this.endId >= 0L;
	}

	public boolean isDescOrder() {
		if (hasEndId() && hasStartId()) {
			return this.startId.compareTo(this.endId) > 0;
		}
		return this.order == Order.desc;

	}

	/**
	 * 页面大小是否有限制
	 * 
	 * @return
	 */
	public boolean isUnLimitPageSize() {
		return this.pageSize == UNLIMIT_PAGE_SIZE;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

}
